package com.balabala.gym_user.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_user.pojo.entity.ShippingAddress;
import com.balabala.gym_user.service.ShippingAddressService;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 何涛
 * @Date: 2023/05/02/ 19:27
 * @description 收货地址controller
 */
@RestController
@RequestMapping("/admin/shippingAddress")
@CrossOrigin
@Slf4j
@Api(tags = "收货地址后台接口")
public class ShippingAddressController {
    @Autowired
    ShippingAddressService shippingAddressService;
    @PostMapping("add")
    @ApiOperation("添加用户收货地址")
    public Result add(@RequestBody ShippingAddress shippingAddress)
    {
        log.info("添加一条用户收货地址:{}",shippingAddress);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,shippingAddressService.save(shippingAddress));
    }
    @DeleteMapping("/deleteById")
    @ApiOperation("删除用户收货地址")
    public  Result deleteById(@RequestParam Long id)
    {
        log.info("删除一条用户收货地址 id:{}",id);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,shippingAddressService.removeById(id));
    }

    @PutMapping("/update")
    @ApiOperation("修改收货地址")
    public Result update(@RequestBody ShippingAddress shippingAddress)
    {
        log.info("修改用户收货地址信息：{}",shippingAddress);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,shippingAddressService.updateById(shippingAddress));
    }
    @GetMapping("/{id}")
    @ApiOperation("根据收货地址id查询收货地址信息")
    public Result findById(@PathVariable Long id)
    {
        log.info("收货地址，id:{}",id);

        return Result.ok(ResultEnum.GENERALLY_SUCCESS,shippingAddressService.getById(id));
    }
    @GetMapping("/findUserAllShippingAddress")
    @ApiOperation("查询用户所有收获地址")
    public Result findUserAllShippingAddress(@RequestParam Long userId)
    {
        log.info("查询用户所有收货地址信息，用户id：{}",userId);
        return Result.ok(ResultEnum.GENERALLY_SUCCESS,shippingAddressService.list(new LambdaQueryWrapper<ShippingAddress>().eq(ShippingAddress::getUserId,userId)));
    }


}
